package com.freesky.biz.rights.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freesky.biz.rights.domain.EncryptBatchEntity;
import com.freesky.biz.rights.domain.EncryptCodeEntity;
import com.freesky.biz.rights.domain.RightsBusiEntity;
import com.freesky.biz.rights.mapper.EncryptBatchMapper;
import com.freesky.biz.rights.mapper.EncryptCodeMapper;
import com.freesky.biz.rights.pojo.EncryptCodePo;
import com.freesky.biz.rights.service.IEncryptBatchService;
import com.freesky.common.constant.CommonConstants;
import com.freesky.common.constant.UserConstants;
import com.freesky.common.utils.HashIdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
public class EncryptBatchServiceImpl extends ServiceImpl<EncryptBatchMapper, EncryptBatchEntity>  implements IEncryptBatchService {

    @Autowired
    private EncryptBatchMapper encryptBatchMapper;

    @Autowired
    private EncryptCodeMapper encryptCodeMapper;

    @Override
    public boolean checkBatchNameUnique(EncryptBatchEntity entity) {
        EncryptBatchEntity info = encryptBatchMapper.checkBatchNameUnique(entity.getBatchName());
        if((info != null) && !info.getId().equalsIgnoreCase(entity.getId())){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }


    @Override
    public Page selectEncryptBatchPage(Page page, EncryptBatchEntity entity) {
        return encryptBatchMapper.selectEncryptBatchPage(page,entity);
    }

    @Override
    public List<EncryptBatchEntity> selectEncryptBatchList(EncryptBatchEntity entity) {
        return encryptBatchMapper.selectEncryptBatchPage(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addEncryptBatch(EncryptBatchEntity entity) {
        Long maxId = encryptCodeMapper.selectMaxId();
        long startEncryptId = 0;
        if((maxId == null) || (maxId.longValue() == 0L)){
            startEncryptId = CommonConstants.START_ENCRYPT_ID;
        } else {
            startEncryptId = maxId.longValue() + 1;
        }

        List<EncryptCodeEntity> encryptCodeList = new ArrayList<>();
        for(int i=0;i<entity.getTotalCoupons();i++){
            EncryptCodeEntity encryptCode = new EncryptCodeEntity();
            long id = startEncryptId + i;
            encryptCode.setId(String.valueOf(id));
            encryptCode.setEncryptId(HashIdUtils.encode(id));
            encryptCode.setBatchId(entity.getId());
            encryptCode.setUseFlag("0");
            encryptCodeList.add(encryptCode);
        }
        encryptBatchMapper.insert(entity);
        encryptCodeMapper.batchAddEncryptCode(encryptCodeList);
        return 1;
    }

    @Override
    public int deleteBatchByIds(String[] ids) {
        encryptBatchMapper.deleteBatchByIds(ids);
        return 1;
    }

    @Override
    public Page selectEncrypCodePage(Page page, EncryptCodePo entity) {
        return encryptCodeMapper.selectEncrypCodePage(page,entity);
    }

    @Override
    public List<EncryptCodePo> selectEncrypCodePage(EncryptCodePo entity) {
        return encryptCodeMapper.selectEncrypCodePage(entity);
    }
}
